// 实现这个项目的构建任务
// 没写完
const fs = require('fs')
const loadGruntTasks = require('load-grunt-tasks')
module.exports = grunt => {
    grunt.initConfig({

        //sass转化
        sass: {
            options: {
                sourceMap: true,
                style: 'expanded'
            },
            main: {
                files: {
                    'temp/src/assets/styles/main.css': 'src/assets/styles/main.scss'
                }
            }
        },
        //es6
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: {
                    'temp/src/assets/scripts/main.js': 'src/assets/scripts/main.js'
                }
            }
        },
        
        // swig
        swigtemplates: {
            staging: {
              context: {
                menus: [
                    {
                        name: 'Home',
                        icon: 'aperture',
                        link: 'index.html'
                    },
                    {
                        name: 'Features',
                        link: 'features.html'
                    },
                    {
                        name: 'About',
                        link: 'about.html'
                    },
                    {
                        name: 'Contact',
                        link: '#',
                        children: [
                            {
                                name: 'Twitter',
                                link: 'https://twitter.com/w_zce'
                            },
                            {
                                name: 'About',
                                link: 'https://weibo.com/zceme'
                            },
                            {
                                name: 'divider'
                            },
                            {
                                name: 'About',
                                link: 'https://github.com/zce'
                            }
                        ]
                    }
                ],
                pkg: require('./package.json'),
                date: new Date()
            },
              dest: 'temp/',
              src: ['src/*.html']
            }
          },
          //混淆，压缩js
        uglify: {
            my_target: {//任务1
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src:'**/*.js',
                    dest: 'dist/assets/scripts',
                    ext: '.min.js'
                }]
            }
        },
        //压缩CSS
        cssmin: {
            target:{
                files: [{
                expand: true,
                cwd: 'temp',
                src: ['**/*.css', '!**/*.min.css'],
                dest: 'dist/assets/styles',
                ext: '.min.css'
            }]
          }
        },
        //压缩html
        htmlmin: {
            dynamic: {
                options: {
                    removeComments: true, //移除注释
                    collapseWhitespace: false//无用空格，false为不删除
                },
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src: ['**/*.html'],
                    dest: 'dist'
                }]
            }
        },
        //图片
        imagemin: {
            dist: {
                options: {
                    optimizationLevel: 1 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'src/assets/images/',//原图存放的文件夹
                    src: ['**'], // 优化 img 目录下所有 图片
                    dest: 'dist/assets/images/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },
        //监听
          watch: {
            js: {
              files: ['src/js/*.js'],//监听
              /*回调，babel任务*/
              tasks: ['babel']
            },
            css: {
              files: ['src/scss/*.scss'],
              tasks: ['sass']
            }
          },
        clean: {
            temp: 'temp/**'
        },
        connect: {server: {
            options: {
              port: 8081,
              open:true,
              base:  ['temp', 'src', 'public']
            }
          }
          }
    })

    loadGruntTasks(grunt)
}